<template>
    <div>
        <el-upload
            class="upload-demo"
            ref='upload'
            :http-request="myUpload"
            :show-file-list="false"
            >
            <el-button size="small" type="primary" style="display:block">上传文件</el-button>
        </el-upload>
        <a :href="xlsx" download="模板.xlsx">
        <el-button size="small" type="primary" style="display:block">模板下载</el-button></a>
    </div>
</template>
<script>
import {UploadApi} from '@/api/goods';
export default {
    name:'uploadfile',
    data(){
        return{
            xlsx:''
        }
    },
    mounted() {
         let upload_div = this.$refs.upload.$children[0];
            upload_div.$el.style.border="0px"
            upload_div.$el.style.width="100%"
            upload_div.$el.style.height="50px"
    },
    created() {
        console.log('created uploader')
        this.xlsx=location.href.slice(0,-13)+'file/模板.xlsx'
    },
    methods:{
         myUpload(param){
                console.log('myUpload...');
                  let extension = param.file.name.substring(param.file.name.lastIndexOf('.')+1)
                    if(extension !== 'xlsx') {
                        this.$message.warning({
                        title: '警告',
                        message: `只能上传Excel2017（即后缀是.xlsx）的文件`
                        });
                        return
                    }
                 const formData = new FormData()
                formData.append('file', param.file)
                    console.log(formData.get('file'))
                    UploadApi(formData).then(response => {
                        console.log(response)
                        if(response.code==0){
                            this.$message.success('上传成功')
                            
                        }else{
                            this.$message.error("上传失败,"+response.msg)
                        }
                    }).catch(response => {
                        console.log(response)
                        this.$message.error("上传失败,"+response.msg)
                    })
            }
    }
}
</script>